﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Web.SessionState;
using System.ComponentModel;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.Web.Security;
using System.Runtime.InteropServices;
namespace iResume
{
    public partial class recruiterViewJobSeeker : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList recruiterViewJobSeeker = new ArrayList();

            Session["email"] = lblEmail.Text;
            string jsemail = (string)(Session["jsusername"]);

            recruiterViewJobSeeker = DBManager.retrievejs(jsemail);
            JobSeeker JsView = new JobSeeker();
            JsView = (JobSeeker)recruiterViewJobSeeker[0];
            Image1.ImageUrl = "jobSeekerDisplayImg.ashx?id=" + JsView.EmailAdd;

            Label7.Text = JsView.FullName;
            Label1.Text = JsView.Jobcategory;
            Label3.Text = JsView.Profile;
            Label4.Text = JsView.Education;
            Label5.Text = JsView.Skills;
            Label6.Text = JsView.Workexperience;
            lblEmail.Text = JsView.EmailAdd;
            


        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            ArrayList sendResumes = new ArrayList();
            string username = (string)(Session["username"]);
            if (username == null)
            {
                Response.Redirect("loginpage.aspx");
            }
            sendResumes = DBManager.retrieveRecruiter(username);
            Recruiter sendRecruiter = new Recruiter();
            sendRecruiter = (Recruiter)sendResumes[0];

            Session["email"] = lblEmail.Text;
            string email = (string)(Session["email"]);


            //create the message
            MailMessage mail = new MailMessage();
            //add the email address we will be sending the message to
            mail.To.Add(email);
            //add our email here
            mail.From = new MailAddress("iResumee@gmail.com");
            //email's subject
            mail.Subject = username + " " + "Resume";
            //email's body, this is going to be html. note that we attach the image as using cid

            //The respective jobseeker has requested to post their resume to your job post 
            //and please follow up on the respective jobseeker.

            mail.Body = "Hi" + " " + Label7.Text + ",<br>" +
                        "<br>The respective jobseeker has requested to post their resume to your job post and please follow up on the respective jobseeker. <br>" +
                        "<br>" + "<b>" + "Company Information" + "</b>" + "</br>" +
                        "<br>" + "Company URL: " + sendRecruiter.CompanyURL + "</br>" +
                        "<br>" + "Company Address: " + sendRecruiter.CompanyAddress + "</br>" +
                        "<br>" + "<b>" + "Company Profile" + "</b>" + "</br>" +
                        "<br>" + "Company Profile: " + sendRecruiter.CompanyProfile + "</br>" +
                        "<br>" + "<b>" + "Personal Incharge Information" + "</b>" + "</br>" +
                        "<br>" + "Name: " + sendRecruiter.PersonInCharge + "</br>" +
                        "<br>" + "Email: " + username + "</br>" +
                        "<br>" + "Contact Number: " + sendRecruiter.ContactNum + "</br>" +
                        "<br>" + "Position: " + sendRecruiter.Position + "</br>" +
                        "<br>" + "Department: " + sendRecruiter.Department + "</br>";

            //set email's body to html
            mail.IsBodyHtml = true;

            
            //setup our smtp client, these are Gmail specific settings
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true; //ssl must be enabled for Gmail
            //our Gmail account credentials
            NetworkCredential credentials = new NetworkCredential("iResumee@gmail.com", "yiweileeling");
            //add credentials to our smtp client
            client.Credentials = credentials;

            try
            {
                //try to send the mail message
                client.Send(mail);
                demo.Visible = true;
                btnSend.Enabled = false;
             
         

            }
            catch
            {
                
            }
            
        }

        
        private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            String strConnString = System.Configuration.ConfigurationManager
            .ConnectionStrings["iResumeConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
            catch
            {
                return null;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
        }
        private void download(DataTable dt)
        {
            
                Byte[] bytes = (Byte[])dt.Rows[0]["data"];
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //Response.ContentType = "application/vnd.ms-word";
                Response.AddHeader("content-disposition", "attachment;filename="
                + dt.Rows[0]["Name"].ToString());

                byte[] tmp = new byte[bytes.Length - 1];
                Array.Copy(bytes, tmp, bytes.Length - 1);
                bytes = tmp;

                Response.BinaryWrite(tmp);
                Response.Flush();
                Response.End();
            
            
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {

            Session["email"] = lblEmail.Text;
            string email = (string)(Session["email"]);

            string strQuery = "select Name, data from JobSeeker where emailAdd=@email";
            SqlCommand cmd = new SqlCommand(strQuery);
            // cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1;
            cmd.Parameters.AddWithValue("@email", email);
            DataTable dt = GetData(cmd);
            if (dt.Rows[0]["data"] != DBNull.Value)
            {
                download(dt);
            }
            else
            {
                lbla.Visible = true;

            }
            
        }
       
    }
}